package cn.edu.sgu.www.mhxysy.service.children_equipment.impl;

import cn.edu.sgu.www.mhxysy.dto.children_equipment.ChildrenEquipmentDTO;
import cn.edu.sgu.www.mhxysy.entity.children_equipment.ChildrenEquipment;
import cn.edu.sgu.www.mhxysy.enums.WearStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.children_equipment.ChildrenEquipmentMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.children_equipment.ChildrenEquipmentPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.children_equipment.ChildrenEquipmentService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.children_equipment.ChildrenEquipmentVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChildrenEquipmentServiceImpl implements ChildrenEquipmentService {

    private final ChildrenEquipmentMapper childrenEquipmentMapper;

    @Autowired
    public ChildrenEquipmentServiceImpl(ChildrenEquipmentMapper childrenEquipmentMapper) {
        this.childrenEquipmentMapper = childrenEquipmentMapper;
    }

    @Override
    public void wear(String childrenId, String equipmentId) {
        ChildrenEquipment childrenEquipment = childrenEquipmentMapper.selectById(equipmentId);

        // 获取装备的穿戴状态
        Integer wearStatus = childrenEquipment.getWearStatus();

        if (WearStatus.WCD.getValue().equals(wearStatus)) {
            UpdateWrapper<ChildrenEquipment> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("id", equipmentId);
            updateWrapper.eq("wear_status", WearStatus.WCD.getValue());

            updateWrapper.set("children_id", childrenId);
            updateWrapper.set("wear_status", WearStatus.YCD.getValue());

            childrenEquipmentMapper.update(updateWrapper.getEntity(), updateWrapper);
        } else {
            UpdateWrapper<ChildrenEquipment> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("id", equipmentId);
            updateWrapper.eq("wear_status", WearStatus.YCD.getValue());

            updateWrapper.set("children_id", null);
            updateWrapper.set("wear_status", WearStatus.WCD.getValue());

            childrenEquipmentMapper.update(updateWrapper.getEntity(), updateWrapper);
        }
    }

    @Override
    public void insert(ChildrenEquipmentDTO chongwuEquipmentDTO) {
        ChildrenEquipment childrenEquipment = toEntity(chongwuEquipmentDTO);

        childrenEquipment.setId(childrenEquipment.uuid());

        childrenEquipmentMapper.insert(childrenEquipment);
    }

    @Override
    public void deleteById(String equipmentId) {
        childrenEquipmentMapper.deleteById(equipmentId);
    }

    @Override
    public void updateById(ChildrenEquipmentDTO chongwuEquipmentDTO) {
        if (StringUtils.isNullOrEmpty(chongwuEquipmentDTO.getId())) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, "ID不能为空");
        }

        ChildrenEquipment childrenEquipment = toEntity(chongwuEquipmentDTO);

        childrenEquipmentMapper.updateById(childrenEquipment);
    }

    @Override
    public List<ChildrenEquipment> selectAll() {
        return childrenEquipmentMapper.selectList(null);
    }

    @Override
    public ChildrenEquipment selectById(String id) {
        return childrenEquipmentMapper.selectById(id);
    }

    @Override
    public Page<ChildrenEquipment> selectByPage(ChildrenEquipmentPager pager) {
        QueryWrapper<ChildrenEquipment> queryWrapper = Pager.getQueryWrapper(pager);
        Page<ChildrenEquipment> page = Pager.ofPage(pager);

        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getRoleId()),
                "role_id", pager.getRoleId()
        );
        queryWrapper.eq(
                pager.getChildId() != null,
                "child_id", pager.getChildId()
        );
        queryWrapper.eq(
                pager.getSkillId() != null,
                "skill_id", pager.getSkillId()
        );
        queryWrapper.eq(
                pager.getCategoryId() != null,
                "category_id", pager.getCategoryId()
        );

        return childrenEquipmentMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<ChildrenEquipmentVO> getWeardEquipments(String childrenId) {
        return childrenEquipmentMapper.getWeardEquipments(childrenId);
    }

    @Override
    public List<ChildrenEquipment> selectList(Integer type, String childrenId) {
        return childrenEquipmentMapper.select(type, childrenId);
    }

    private ChildrenEquipment toEntity(ChildrenEquipmentDTO childrenEquipmentDTO) {
        ChildrenEquipment chongwuEquipment = new ChildrenEquipment();

        chongwuEquipment.setId(childrenEquipmentDTO.getId());
        chongwuEquipment.setForger(childrenEquipmentDTO.getForger());
        chongwuEquipment.setRoleId(childrenEquipmentDTO.getRoleId());
        chongwuEquipment.setSkillId(childrenEquipmentDTO.getSkillId());
        chongwuEquipment.setChildrenId(childrenEquipmentDTO.getChildrenId());
        chongwuEquipment.setWearStatus(childrenEquipmentDTO.getWearStatus());
        chongwuEquipment.setCategoryId(childrenEquipmentDTO.getCategoryId());

        return chongwuEquipment;
    }

}